<?php
# PLEASE DO NOT MODIFY THIS WITHOUT READING THE INSTRUCTIONS
# NEVER TOUCH SECTION MARKED AS "DO NOT CHANGE" Line 6 to Line 97
# CHANGE ONLY SECTION MARKED AS "CHANGE   ONLY YOUR SECTION" Line 98 to Clas } 
# BUT YOUR SECTION ONLY. e.i ONLY THE FUNCTIOS CREATED BY YOU	

class bou
{
	################################   DO NOT CHANGE   #################################
	//MyQql Property
	var $conn;
	var $result;
	
	//BOU Property
	//IN_MSG_ID=11254&MOBILENO=8801711505676&BODY=Test Message&ORIGIN_PORT=2000
	var	$IN_MSG_ID;
	var $MOBILENO;
	var $BODY;
	var $ORIGIN_PORT;

	var $your_out_msg_id;
	var $sms;
    var $m_date;
	var $a_session;
	
	//question
	
	var $qid;
	var $c_ans;
	var $c_op;
	var $op1;
	var $op2;
	var $op3;
	var $op4;
	var $ques;
	var $res;
	var $les;
	var $dic_m;
	var $dic_w;
	var $dic_s;
	var $q_a;
	var $mg;
	################################   DO NOT CHANGE   #################################
									//MySql Method
									
	//Connect the Server
	function MySQL($host="localhost", $user="root", $pass="",$bd="bou")
	//function MySQL($host, $user, $pass,$bd)
	{
		//echo "$host, $user , $pass, $bd";
		$this->conn = mysql_connect($host,$user, $pass) or die(mysql_error()) 
				or die('Could not connect: ' . mysql_error());
				
		$this->select_db($bd);
	}
	
	//Connect to badabase
	function select_db($bd)
	{
				$db=mysql_select_db($bd,$this->conn) or die(mysql_error())
						or die('Could not connect to '. $bd .' ' . mysql_error());
	}
	
	//Generate Querey
	function sql($SQL)
	{

		$this->result = mysql_query($SQL)
					or die('SQL Error<br>' .$SQL.' '. mysql_error());
		
	}
	################################   DO NOT CHANGE   #################################
									//BOU Property
	//Print Message
	function myPrint($myMsg)
	{
		echo "<br>";
		echo $myMsg;
		echo "<br>";
	}


	//Recieve the parameter values
	function bou($IN_MSG_ID, $MOBILENO, $BODY , $ORIGIN_PORT, $your_out_msg_id)
	{
		
		$this->IN_MSG_ID = $IN_MSG_ID;
		$this->MOBILENO = $MOBILENO;
		$this->BODY = $BODY;
		$this->ORIGIN_PORT = $ORIGIN_PORT;
		$this->your_out_msg_id = $your_out_msg_id;
		
			
	}
	
	//XML
	function xml()
	{
	header('Content-Type: text/xml');
	?>
<MT>

<OUT_MSG_ID><?php echo $this->your_out_msg_id?></OUT_MSG_ID>
	
<MOBILENO><?php echo $this->MOBILENO?></MOBILENO>
	
<IN_MSG_ID><?php echo $this->IN_MSG_ID?></IN_MSG_ID>
	
<REPLY_TEXT><?php echo $this->sms?></REPLY_TEXT>
	
<MSG_TYPE>4</MSG_TYPE>
	
<SEND_PORT><?php echo $this->ORIGIN_PORT?></SEND_PORT>
	
</MT>
	<?php
	}
	
	
	
	################################   CHANGE   ONLY YOUR SECTION  #################################
									   //All Parsing Functions 
									   
	/*******************************           IMRAN               ********************************/
	//dictionary
	function dictionary($wrd)
	{
	

		
		$SQL = "SELECT * FROM dictionary where word ='$wrd'";
	
		$this->sql($SQL);
	
		
		while($row = mysql_fetch_array($this->result))
  		{  		
  			
  		
		$dic_m=$row['meaning'];
		$dic_w=$row['word'];
		$dic_s=$row['sentence'];
		}
		
		if ($dic_m=="") 
		
		{
		$this->sms= "No word found";
		}
		
		else
		{
		
		$this->sms= "Word".": ".$dic_w.". "."Meaning".": ".$dic_m." ."."Sentence".": ".$dic_s;
		
		}
		
	}
	
	//feedBack
	function feedBack($wrd)
	{
		$m_date= date("d-m-y");
		
		
		$SQL= "SELECT * FROM attendence where phn_no ='$this->MOBILENO'";
		$this->sql($SQL);
		
		while($row = mysql_fetch_array($this->result))
  		{  		//echo $row['msg']."<br>";
  			$a_session=$row['session'];
			 
  		}
		
		
		$SQL = "INSERT INTO feedback (id,phn_no,comment,m_date,session) VALUES ('','$this->MOBILENO', '$wrd','$m_date','$a_session')";
		$this->sql($SQL);
		
		$this->sms="Good to have your opinion, Thank you";
	}
	//rules
	function rules()
	{
		
		$SQL = "SELECT * FROM rulz where id='1'";
		
		$this->sql($SQL);
		while($row = mysql_fetch_array($this->result))
  		{  		
  			$this->sms = $row['msg']; 
  		}
		$this->xml();
	}
	
	//TV schedule
	function schedule()
	{
		
		$SQL = "SELECT schedule FROM sch where s_id='1'";
		
		$this->sql($SQL);
		while($row = mysql_fetch_array($this->result))
  		{  		
  			$this->sms = $row['schedule']; 
  		}
		$this->xml();
	}
	
	//Attendence
	function attn($wrd)
	{
		$m_date= date("d-m-y");
		$SQL = "INSERT INTO attendence (ID,phn_no,session,m_date) VALUES ('','$this->MOBILENO', '$wrd','$m_date')";
		$this->sql($SQL);
		
		$this->sms="Your attendence for session  "."$wrd"."  has been recorded, Thank you";
		$this->xml();
	}
	
	//Homework
	function homework($wrd)
	{
		$m_date= date("d-m-y");
		//$SQL = "INSERT INTO attendence (ID,phn_no,session,m_date) VALUES ('','$this->MOBILENO', '$wrd','$m_date')";
		
		$SQL= "SELECT * FROM attendence where phn_no ='$this->MOBILENO'";
		$this->sql($SQL);
		
		while($row = mysql_fetch_array($this->result))
  		{  		//echo $row['msg']."<br>";
  			$a_session=$row['session'];
			 
  		}
		$SQL = "INSERT INTO homework (id,phn_no,hw,m_date,session) VALUES ('','$this->MOBILENO', '$wrd','$m_date','$a_session')";
		
		$this->sql($SQL);
		
		$this->sms= "Thank you for trying the homework,Good attempt!!!";
		
		$this->xml();
	}
	
	//Que2
	
	
	function que($wrd)
	{
		$m_date= date("d-m-y");
		$q_a=substr($wrd,-1,1);
		
		$SQL = "INSERT INTO temp_que (id,Phn_no,que,ans,m_date) VALUES ('','$this->MOBILENO', '$wrd','$q_a','$m_date')";
		$this->sql($SQL);
		
		$this->sms="Your asnswer is saved!!! Thank you";
		
		//$this->sms="$q_a";
		$this->xml();
	}
	
	// temp question for answer
	
	function temp_que($mg,$les)
	
	{
	$m_date= date("d-m-y");
		
		
		$SQL="SELECT * FROM question q WHERE q.Qid NOT IN (SELECT a.Qid FROM answer a WHERE a.Phn_no = '$this->MOBILENO' AND a.lesson = '$les' ) and q.lesson='$les'";
		
		$this->sql($SQL);
		
		while($row = mysql_fetch_array($this->result))
  		{  		
  			$qid=$row['Qid'];
			$c_ans=$row['c_ans'];
			$c_op=$row['c_op'];
			
			$ques=$row['question'];
			$op1=$row['op1'];
			$op2=$row['op2'];
			$op3=$row['op3'];
			$op4=$row['op4'];
			
			
			 
  		}
		
		if($qid=="") 
		{
		 
		$this->sms="No lesson found";
		$this->xml();
		
		}
	else
	{	
		$SQL = "INSERT INTO temp_table(id,Qid,c_ans,c_op,phn_no,lesson) VALUES ('','$qid','$c_ans','$c_op','$this->MOBILENO','$les')";
		
		$this->sql($SQL);
		
		
		
		
		$this->sms="$mg"."    "."$ques"."a)"."$op1"."b)"."$op2"."c)"."$op3"."d)"."$op4";
		$this->xml();
	}
	
}	
	
	//Question
	
	
	function question($wrd)
	{
		$m_date= date("d-m-y");
		//$SQL = "Select q.Qid,q.question,q.op1,q.op2,q.op3,q.op4,q.c_ans,q.c_op from Question q where q.Qid not in(select a.Qid from answer a where a.Phn_no=' $this->MOBILENO') And q.lesson='$wrd'";
		//$SQL = "Select q.Qid,q.question,q.op1,q.op2,q.op3,q.op4,q.c_ans,q.c_op from Question q where q.Qid in(select a.Qid from answer a where a.Phn_no=' $this->MOBILENO' and a.lesson='$wrd')";
		
		$SQL="SELECT * FROM question q WHERE q.Qid NOT IN (SELECT a.Qid FROM answer a WHERE a.Phn_no = '$this->MOBILENO' AND a.lesson = '$wrd' ) and q.lesson='$wrd'";
		//echo $SQL;
		//echo $SQL;
		$this->sql($SQL);
		
		while($row = mysql_fetch_array($this->result))
  		{  		
  			$qid=$row['Qid'];
			$c_ans=$row['c_ans'];
			$c_op=$row['c_op'];
			
			$ques=$row['question'];
			$op1=$row['op1'];
			$op2=$row['op2'];
			$op3=$row['op3'];
			$op4=$row['op4'];
			//$les=$row['les'];
			
			 
  		}
		
		if($qid=="") 
		{
		 
		$this->sms="No lesson found";
		$this->xml();
		
		}
	else
	{	
		$SQL = "INSERT INTO temp_table(id,Qid,c_ans,c_op,phn_no,lesson) VALUES ('','$qid','$c_ans','$c_op','$this->MOBILENO','$wrd')";
		//echo $SQL;
		$this->sql($SQL);
		
		
		//$this->sms="Thank you for your attendence in the class"."$session";
		
		$this->sms="$ques"."a)"."$op1"."b)"."$op2"."c)"."$op3"."d)"."$op4";
		$this->xml();
	}
	
	
	
	}
	// Answer
	function answer($wrd)
	{
		$m_date= date("d-m-y");
		$SQL = "select c_op,Qid,lesson from temp_table where Phn_no='$this->MOBILENO' order by id";
		
		//echo $SQL;
		$this->sql($SQL);
		
		while($row = mysql_fetch_array($this->result))
  		{  		//echo $row['msg']."<br>";
  			$qid=$row['Qid'];
			$c_op=$row['c_op'];
			$les=$row['lesson'];
		}
		
		//echo $qid; 
		//echo $c_op;
		//echo $wrd;
		
		
		if ($c_op== $wrd)
		{
		 //$this->sms="ur answer is correct";
		$mg="ur answer is correct";
		
		   
		   $res='1';
		  
		  //next question
		  
		 // $this->question($les);
		  
		  
		  $SQL = "INSERT INTO answer(id,Phn_no,Qid,result,ans,m_date,lesson) VALUES ('','$this->MOBILENO','$qid','$res','$wrd','$m_date','$les')";
			//echo $SQL;
		$this->sql($SQL);
		
		$SQL="delete from temp_table where Phn_no='$this->MOBILENO'";
		$this->sql($SQL);
		
		$this->temp_que($mg,$les);
		//$this->xml();  
		  
		  
		  }
		  else
		  
		  {
		  //$this->sms="ur answer is wrong";
	
		  $mg="ur answer is wrong";
		
		 
		  $res='0';
		 
		 $SQL = "INSERT INTO answer(id,Phn_no,Qid,result,ans,m_date,lesson) VALUES ('','$this->MOBILENO','$qid','$res','$wrd','$m_date','$les')";
		//echo $SQL;
		$this->sql($SQL);
		
		$SQL="delete from temp_table where Phn_no='$this->MOBILENO'";
		$this->sql($SQL);
		
		 $this->temp_que($mg,$les);
	//	$this->xml();
		 
		  }
		
		
		
		
		
		//$this->sms="Thank you for your attendence in the class"."$session";
		
		
	}
	
	
	/*******************************           RIZVEE               ********************************/
	
	/*******************************           SHEAHAD              ********************************/
	
	#################################################################################################
}

//$obj = new bou;
?>